package leertaak2;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Class to write the data to a CSV file
 * @author marcovellinga
 *
 */
public class Writer  {
	
	// Variables
	private FileWriter fileWriter;
	private PrintWriter writer;
	private ArrayList<Data> data;
	
	/**
	 * Constructor with the arraylist that contains the data
	 * @param data
	 * @throws IOException
	 */
	public Writer(ArrayList<Data> data) throws IOException{
		this.data = data;
	}
	
	/**
	 * Append the data to the back of the existing data in the file
	 */
	public void insertData() {
		
			for(int i = 0; i < data.size(); i++){
				
				Data realData = data.get(i);
				try {
					fileWriter = new FileWriter("/Users/marcovellinga/Desktop/WriterProject/" + realData.getStn()  + ".csv", true);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				writer = new PrintWriter(fileWriter);
				
				writer.append(realData.getDate()); // 1
			    writer.append(',');
			    writer.append(realData.getTime()); // 2
			    writer.append(',');
			    writer.append(realData.getTemp()); // 3
			    writer.append(',');
			    writer.append(realData.getDewp()); // 4
		        writer.append(',');
		        writer.append(realData.getStp()); // 5
			    writer.append(',');
			    writer.append(realData.getSlp()); // 6
			    writer.append(',');
			    writer.append(realData.getVisib()); // 7
			    writer.append(',');
			    writer.append(realData.getWdsp()); // 8
			    writer.append(',');
			    writer.append(realData.getPrcp()); // 9
			    writer.append(',');
			    writer.append(realData.getSndp()); // 10
			    writer.append(',');
			    writer.append(realData.getFrshtt()); // 11
			    writer.append(',');
			    writer.append(realData.getCldc()); // 12
			    writer.append(',');
			    writer.append(realData.getWnddir()); // 13
			    writer.append('\n');
		        writer.flush();
		        writer.close();
			}	
	}
}
